package com.chima.action;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.chima.domain.AddWork;
import com.chima.domain.Employees;
import com.chima.domain.Job;
import com.chima.domain.JobDetail;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.service.AddWorkService;
import com.chima.service.JobDetailService;
import com.chima.service.JobService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import flexjson.JSONSerializer;
import flexjson.transformer.DateTransformer;

public class AddWorkAction extends ActionSupport implements ModelDriven<AddWork> {
	
	AddWork addWork=new AddWork();
	public AddWork getModel() {
		
			return addWork;
	}
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private boolean success=false ;
	private JobDetailService jobDetailService;
	private AddWorkService addWorkService;
	private JobService jobService;
	private List<AddWork> addWorks;
	
	public void setJobService(JobService jobService) {
		this.jobService = jobService;
	}
	private double  core;
	private int  tastproperty;
	
	public double getCore() {
		return core;
	}
	public void setCore(double core) {
		this.core = core;
	}
	public int getTastproperty() {
		return tastproperty;
	}
	public void setTastproperty(int tastproperty) {
		this.tastproperty = tastproperty;
	}
	public void setJobDetailService(JobDetailService jobDetailService) {
		this.jobDetailService = jobDetailService;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public AddWorkService getAddWorkService() {
		return addWorkService;
	}
	public void setAddWorkService(AddWorkService addWorkService) {
		this.addWorkService = addWorkService;
	}
	public List<AddWork> getAddWorks() {
		return addWorks;
	}
	public void setAddWorks(List<AddWork> addWorks) {
		this.addWorks = addWorks;
	}
	
	public String  ListAddWork() throws Exception{
	   PageRequestBean pageRequestBean = new PageRequestBean();
	   DetachedCriteria criteria = DetachedCriteria.forClass(AddWork.class);
		Employees  e =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
		if(!e.getName().equals("admin")){
			criteria.add(Restrictions.eq("employee",e));
		}
		pageRequestBean.setCriteria(criteria);
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean =addWorkService.findAddWork(pageRequestBean);
		addWorks=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.exclude("*.class");
        jsonSerializer.transform(new DateTransformer("yyyy-MM-dd"),Date.class);
//        jsonSerializer.transform(new DateTransformer("yyyy-MM-dd"),Date.class);
        String resultJson1 = jsonSerializer.deepSerialize(addWorks);  
        String resultJson2 =jsonSerializer.serialize(total);
        String jsonString = "{total:"+resultJson2.toString()+",addWorks:"+resultJson1.toString()+"}";
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	
  }
	public void addAddWork() throws Exception{
		 Employees  existEmployee =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
		try {
			AddWork a=new AddWork();
			a.setName(addWork.getName());
			a.setDepartment(existEmployee.getDepartment());
			a.setEmployee(existEmployee);
			a.setStarttime(addWork.getStarttime());
			a.setEndtime(addWork.getEndtime());
			a.setContent(addWork.getContent());
			a.setState(0);
			addWorkService.saveAddWork(a);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
		public void editAddWork() throws Exception{
			AddWork w=addWorkService.findById(addWork.getId());
			w.setStarttime(addWork.getStarttime());
			w.setName(addWork.getName());
			w.setEndtime(addWork.getEndtime());
			w.setContent(addWork.getContent());
			addWorkService.updateAddWork(w);
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	public String deleteAddWork(){
		addWorkService.deleteAddWork(addWork.getId());
		return NONE;
	}
	public String  ListWaitAddWork() throws Exception{
		   PageRequestBean pageRequestBean = new PageRequestBean();
		   DetachedCriteria criteria = DetachedCriteria.forClass(AddWork.class);
			Employees  e =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
			if(!e.getName().equals("admin")){
				criteria.add(Restrictions.eq("department",e.getDepartment()));
			}
			pageRequestBean.setCriteria(criteria);
			pageRequestBean.setStart(start);
			pageRequestBean.setLimit(limit);
			pageRequestBean.setPage(page);
			PageResponseBean pageResponseBean =addWorkService.findAddWork(pageRequestBean);
			addWorks=pageResponseBean.getRows();
	        int total=0;
	        total =pageResponseBean.getTotal();
			// 输出json内容到客户端
	        JSONSerializer jsonSerializer = new JSONSerializer();
	        jsonSerializer.include("*.id","*.content");
	        jsonSerializer.exclude("*.class");
	        jsonSerializer.transform(new DateTransformer("yyyy-MM-dd"),Date.class);
	        String resultJson1 = jsonSerializer.deepSerialize(addWorks);  
	        String resultJson2 =jsonSerializer.serialize(total);
	        String jsonString = "{total:"+resultJson2.toString()+",addWorks:"+resultJson1.toString()+"}";
			ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
			ServletActionContext.getResponse().getWriter().print(jsonString);
			return NONE;
		
	  }
// 审批加班
	public String ShenpiWork() throws Exception{
		   Employees  e =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
		  AddWork w=addWorkService.findById(addWork.getId());
		 if(addWork.getState()==1){
			      Job  job=new Job();
		            job.setCore(core);
		            job.setEmployee(e);
		            job.setName(w.getName());
		            job.setCreatDate(w.getStarttime());
		            job.setJobstyle(2);
		            job.setNanyi(tastproperty);
		            job.setState(2);
		            jobService.saveJobs(job);
		         Job  job1=jobService.findJobByNameAndDateAndDept(w.getName(), w.getStarttime(), e.getId());
			     JobDetail  j=new JobDetail();
			     j.setStep(1);
			     j.setCore(core);
			     j.setActualcore(core);
			     j.setPingji(1);
			     j.setShu(1.0);
			     j.setState("1");
			     j.setHandleState("1");
			     j.setJob(job1);
			     j.setName(w.getName());
			     j.setJobRequirement(w.getContent());
			     j.setTastproperty(job1.getNanyi());
			     j.setStartdate(w.getStarttime());
			     j.setPlanCompleteDate(w.getEndtime());
			     j.setEmployee(w.getEmployee());
			     j.setDepartment(w.getDepartment());
			     jobDetailService.saveJobDetail(j);
				 w.setAdvice(addWork.getAdvice());
				 w.setState(addWork.getState());
				 addWorkService.updateAddWork(w);
			    this.success=true;
				JSONSerializer jsonSerializer = new JSONSerializer();
				String resultJson =jsonSerializer.serialize(success);
				String jsonString = "{success:"+resultJson.toString()+"}";
				System.out.println(jsonString);
				ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
				ServletActionContext.getResponse().getWriter().print(jsonString);
		 }if(addWork.getState()==2){
				 w.setAdvice(addWork.getAdvice());
				 w.setState(addWork.getState());
				 addWorkService.updateAddWork(w);
			    this.success=true;
				JSONSerializer jsonSerializer = new JSONSerializer();
				String resultJson =jsonSerializer.serialize(success);
				String jsonString = "{success:"+resultJson.toString()+"}";
				ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
				ServletActionContext.getResponse().getWriter().print(jsonString);
		 }
		   
		return NONE;
	}
	public String Quxiaoshenpi() {
		Employees  e =(Employees) ServletActionContext.getRequest().getSession().getAttribute("existEmployee");
		  AddWork w=addWorkService.findById(addWork.getId());
		  if(w.getState()==0){
				throw new RuntimeException("待审批");
		  }
		  if(w.getState()==2){
			   w.setState(0);
			   w.setAdvice("");
			   addWorkService.updateAddWork(w);
		  }if(w.getState()==1){
			   w.setState(0);
			   w.setAdvice("");
			  Job  job=jobService.findJobByNameAndDateAndDept(w.getName(), w.getStarttime(), e.getId());
			  jobDetailService.deleteJobDetailByjobId(job.getId());
			  jobService.deleteJobs(job.getId());
			  addWorkService.updateAddWork(w);
		  }
		  
		return NONE;
	}
	public String JianChaWork() throws Exception{
		  AddWork w=addWorkService.findById(addWork.getId());
			if(w.getState()==0){
				this.success=true;
				JSONSerializer jsonSerializer = new JSONSerializer();
				String resultJson =jsonSerializer.serialize(success);
				String jsonString = "{success:"+resultJson.toString()+"}";
				System.out.println(jsonString);
				ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
				ServletActionContext.getResponse().getWriter().print(jsonString);
				
			}else{
				throw new RuntimeException("已经审批");
			}
		return NONE;
	}

}
